package com.edgar.log4j.filter;

import java.util.Vector;

import org.apache.log4j.Appender;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.ErrorHandler;
import org.apache.log4j.spi.LoggingEvent;

public class CustomerErrorHandler implements ErrorHandler {

	private Appender primary;
	private Appender backup;
	private Vector<Logger> loggers = new Vector<Logger>();

	@Override
	public void error(String message) {
		error(message, null, 0, null);
	}

	@Override
	public void error(String message, Exception e, int errorCode) {
		error(message, e, errorCode, null);
	}

	@Override
	public void error(String message, Exception e, int errorCode,
			LoggingEvent event) {
		for (int i = 0; i < loggers.size(); i++) {
			Logger l = loggers.elementAt(i);
			l.removeAppender(primary);
			l.addAppender(backup);
			l.error(message, e);
		}
	}

	@Override
	public void setAppender(Appender appender) {
		this.primary = appender;
	}

	@Override
	public void setBackupAppender(Appender appender) {
		this.backup = appender;
	}

	@Override
	public void setLogger(Logger logger) {
		if (logger != null) {
			loggers.add(logger);
		}
	}

	@Override
	public void activateOptions() {
		// TODO Auto-generated method stub

	}

}
